#include"Date.h"
#include<string>
#include<iostream>
#include<cstring>
using namespace std;

Date::Date()
{
    year=2015;
    month=1;
    day=1;
}
Date::Date(char strDate[])
{
    char y[5];
    int i=0;
    while(strDate[i]!='\0'&&strDate[i]!=':')
    {
        y[i]=strDate[i];
        i++;
        y[i]='\0';
        year=atoi(y);
        if(strDate[i]!='\0')
            i++;
        char m[3];
        int j=i,k=0;
        while(strDate[i]!='\0'&&strDate[i]!=':')
        {
            m[i-j]=strDate[i];
            i++;
            k++;

        }
        m[k]='\0';
        month=atoi(m);
        if(strDate[i]!='\0')

            i++;
        char d[3];
        j=i,k=0;
        while(strDate[i]!='\0'&&strDate[i]!=':')
        {
            d[i-j]=strDate[i];
            i++;
            k++;
        }
        d[k]='\0';
        day=atoi(d);
    }
}
Date::Date(int Y,int M,int D){}
void Date::setDate(int Y,int M,int D){year=Y; month=M;day=D;}
void Date::show()const
{
    cout<<year<<"(";
    if(isLeapYear())
        cout<<"";
    else cout<<"ƽ";
    cout <<"("<<month<<""<<day<<"";
    cout<<endl;
}
int Date::getYear(){return year;}
void Date::setYear(int y){year=y;}
int Date::getMonth(){return month;}
void Date::setMonth(int M){month=M;}
int Date::getDay(){return day;}
void Date::setDay(int D){day=D;}
char* Date::toString()
{
    char *arr=new char[20];
    char *IntToString(int);
    strcpy (arr,IntToString(year));
    strcat(arr,":");
    strcpy (arr,IntToString(month));
    strcat(arr,":");
    strcpy (arr,IntToString(day));
    return arr;

    }
    int Date::daysPerMonth(int m)const
    {
        m=(m<0)?month:m;
        switch(m)
        {
        case 1:return 31;
        case 2:return isLeapYear(year)?29:28;
        case 3:return 31;
        case 4:return 30;
        case 5:return 31;
        case 6:return 30;
        case 7:return 31;
        case 8:return 31;
        case 9:return 30;
        case 10:return 31;
        case 11:return 30;
        case 12:return 31;
        default:return -1;
        }
    }
    int Date::daysPerYear(int y)const
    {
        y=(y<0)?year:y;
        if(isLeapYear(y)) return 366;
        return 365;
    }
    int Date::compare(const Date &date)const
    {
        if(year>date.year) return  1;
        else if(year<date.month) return -1;
        else {if(month>date.month) return 1;
        else {if(month<date.month) return -1;
        else {if(day<date.day) return 1;
        else if(day<date.day) return -1;
        else return 0;}
        }
    }
    }
    bool Date::isLeapYear(int y)const
    {
        y=(y<0)?year:y;
        if(0==y%400||(0==y%4&&0!=y%100)) return true;
        return false;
    }
    int Date::subDate(const Date &date)const
    {if(compare(date)>0)
    return subSmallDate(date);
    else if(compare(date)<0)
    return -(date.subSmallDate(*this));
    else return 0;
    }
    Date Date::addDays(int days) const
    {
        Date newDate(year,month,day);
        if(days>0)
        {
            for(int i=0;i<days;i++)
                newDate.addOneDay();}

else if(days<0)
    {
        for(int i=0;i<(-days);i++)
            newDate.subOneDay();}
        return newDate;
    }
Date Date::subDays(int days) const{return  addDays(-days);}
//
void Date::addOneDay()
{
    if(++day>daysPerMonth()){day=1;
    month++;}
    if(month>12){month=1;year++;}
}


int Date::subSmallDate(const Date &dat)const {int days=0;
Date date(dat);
while(year>(date.year+1))
    {
        days+=date.daysPerYear();
        date.year++;
    }
    while(month>(date.month+1))
    {
        days+=date.daysPerMonth();
        date.month++;
    }
    while(day>(date.day+1))
    {
        days++;
        date.day++;
    }
    while(compare(date)>0)
    {
        days++;
        date.addOneDay();
    }
    return days;
}
char * IntToChar(int n)
{
switch(n)
{case 0:return "0";
case 1:return "1";
case 2:return "2";
case 3:return "3";
case 4:return "4";
case 5:return "5";
case 6:return "6";
case 7:return "7";
case 8:return "8";
case 9:return "9";
}
    }
    char * IntToString(int n)
    {
        char *ptr=new char[20];
        if(n<10){strcpy(ptr,IntToChar(n));
        return ptr;}
    else{strcpy(ptr,IntToChar(n%10));
    return ptr;}
    }
Date::~Date()
{

}
